package com.bootdo.system.dao;

import com.bootdo.system.domain.Order;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface OrderMapper {

	@Select("select *,(select babyName from tb_baby where id=babyId limit 1) babyName from tb_Order where id = #{id}")
	Order get(Long id);
	
	
	@Select("select *,(select babyName from tb_baby where id=babyId limit 1) babyName  from tb_Order where orderNo = #{orderNo}")
	Order getByNo(String orderNo);
	
	@Select("<script>" +
	"select *,(select babyName from tb_baby where id=babyId limit 1) babyName  from tb_Order " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"orderNo != null and orderNo != ''\">"+ "and orderNo = #{orderNo} " + "</if>" + 
	  		  "<if test=\"orderStatus != null and orderStatus != ''\">"+ "and orderStatus = #{orderStatus} " + "</if>" + 
	  		  "<if test=\"parentsId != null and parentsId != ''\">"+ "and parentsId = #{parentsId} " + "</if>" + 
	  		  "<if test=\"babyId != null and babyId != ''\">"+ "and babyId = #{babyId} " + "</if>" + 
	  		  "<if test=\"courseId != null and courseId != ''\">"+ "and courseId = #{courseId} " + "</if>" + 
	  		  "<if test=\"customerId != null and customerId != ''\">"+ "and customerId = #{customerId} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<Order> list(Map<String,Object> map);
	
	@Select("<script>" +
	"select o.*,"
	+ " (select babyName from tb_baby where id=o.babyId limit 1) babyName "
	+ " from tb_Order o " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"orderNo != null and orderNo != ''\">"+ "and orderNo = #{orderNo} " + "</if>" + 
	  		  "<if test=\"parentsId != null and parentsId != ''\">"+ "and parentsId = #{parentsId} " + "</if>" + 
	  		  "<if test=\"babyId != null and babyId != ''\">"+ "and babyId = #{babyId} " + "</if>" + 
	  		  "<if test=\"courseId != null and courseId != ''\">"+ "and courseId = #{courseId} " + "</if>" + 
	  		  "<if test=\"customerId != null and customerId != ''\">"+ "and customerId = #{customerId} " + "</if>" + 
	  		  "<if test=\"isOver != null and isOver == 1\">"+ "and (orderStatus=-1 or orderStatus=3)" + "</if>" +
	  		  "<if test=\"isOver != null and isOver == 0\">"+ "and (orderStatus=0 or orderStatus=1) " + "</if>" +
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<Order> listByTime(Map<String,Object> map);
	
	
	@Select("select *,(select babyName from tb_baby where id=babyId limit 1) babyName  from tb_Order where DATE_ADD(createTime,INTERVAL 15 MINUTE)<now() and orderStatus=0")
	List<Order> getOutTimeOrders();
	
	@Select("<script>" +
	"select count(*) from tb_Order " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"orderNo != null and orderNo != ''\">"+ "and orderNo = #{orderNo} " + "</if>" + 
	  		  "<if test=\"parentsId != null and parentsId != ''\">"+ "and parentsId = #{parentsId} " + "</if>" + 
	  		  "<if test=\"babyId != null and babyId != ''\">"+ "and babyId = #{babyId} " + "</if>" + 
	  		  "<if test=\"courseId != null and courseId != ''\">"+ "and courseId = #{courseId} " + "</if>" + 
		  	"</where>"+ 
			"</script>")
	int count(Map<String,Object> map);
	
	@Insert("insert into tb_Order (`courseId`, `coursePrice`, `coursePoints`, `orderStatus`, `orderNo`, `parentsId`, `babyId`, `createUserId`, `createTime`"
			+ ", `payTime`, `cancelTime`, `parentName`, `parentPhone`, `customerId`,`payType`,`accountTotal`)"
	+ "values (#{courseId}, #{coursePrice}, #{coursePoints}, #{orderStatus}, #{orderNo}, #{parentsId},#{babyId},#{createUserId}, #{createTime}"
	+ ", #{payTime}, #{cancelTime}, #{parentName}, #{parentPhone}, #{customerId}, #{payType}, #{accountTotal})")
	@Options(useGeneratedKeys=true)
	int save(Order bComments);
	
	@Update("<script>"+ 
			"update tb_Order " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"courseId != null\">`courseId` = #{courseId}, </if>" + 
                    "<if test=\"coursePrice != null\">`coursePrice` = #{coursePrice}, </if>" + 
                    "<if test=\"coursePoints != null\">`coursePoints` = #{coursePoints}, </if>" + 
                    "<if test=\"orderStatus != null\">`orderStatus` = #{orderStatus}, </if>" + 
                    "<if test=\"orderNo != null\">`orderNo` = #{orderNo}, </if>" +    
                    "<if test=\"parentsId != null\">`parentsId` = #{parentsId}, </if>" + 
                    "<if test=\"babyId != null\">`babyId` = #{babyId}, </if>" + 
                    "<if test=\"payTime != null\">`payTime` = #{payTime}, </if>" + 
                    "<if test=\"cancelTime != null\">`cancelTime` = #{cancelTime}, </if>" + 
                    "<if test=\"parentName != null\">`parentName` = #{parentName}, </if>" + 
                    "<if test=\"parentPhone != null\">`parentPhone` = #{parentPhone}, </if>" + 
                    "<if test=\"customerId != null\">`customerId` = #{customerId}, </if>" + 
                    "<if test=\"payType != null\">`payType` = #{payType}, </if>" + 
                    "<if test=\"accountTotal != null\">`accountTotal` = #{accountTotal}, </if>" + 
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(Order bComments);
	
	@Delete("delete from tb_Order where id =#{id}")
	int remove(Long id);
	
	@Delete("<script>"+ 
			"delete from tb_Order where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
	
	
	
	@Update("<script>"+ 
			"update tb_Order " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"orderStatus != null\">`orderStatus` = #{orderStatus}, </if>" + 
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int updateOrderStatus(Map<String,Object> param);
}
